<?php

if(!empty($_POST)) {
    //Existierenden Type veraendern
    if(!empty($_POST['type_bez'])) {

        //Einfache Überprüfung der Eingabeparameter
        $type_bez = $_POST['type_bez'];

        if(is_numeric($_POST['value']))
            $value = $_POST['value'];
        else {
            $error = "Fehler bei Eingabedaten";
        }
        if(is_numeric($_POST['unit_price']))
            $unit_price = $_POST['unit_price'];
        else {
            $error = "Fehler bei Eingabedaten";
        }

        //ToDo: Datum muss noch ueberprueft werden
        $valid_from = $_POST['valid_from'];

        if(isset($_POST['delete']))
            $delete = $_POST['delete'];

        //Evtl. Type loeschen
        $types_xml = simplexml_load_file("./xml/types.xml");
        if($types_xml) {
            $i = 0;
            foreach($types_xml->type as $type) {
                //Type evtl. loeschen
                if(!empty($delete) && $type->attributes()->type_name == $delete) {
                    unset($types_xml->type[$i]);
                    break;
                }
                $i++;
            }
            $types_xml->asXML("./xml/types.xml"); //Neues XML-Dokument speichern
        }

        //Base_Prices aktualiseren oder loeschen
        $base_prices_xml = simplexml_load_file("./xml/base_prices.xml");
        if($base_prices_xml) {
            $i = 0;
            foreach($base_prices_xml->base_price as $base_price) {
                if(!empty($delete) && $base_price->attributes()->type == $delete) {
                    unset($base_prices_xml->base_price[$i]);
                    break;
                }
                else if($base_price->attributes()->type == $type_bez) {
                    $base_prices_xml->base_price[$i]->attributes()->value = $value;
                    $base_prices_xml->base_price[$i]->attributes()->valid_from = $valid_from;
                    $base_prices_xml->base_price[$i]->attributes()->unit_price = $unit_price;
                }
                $i++;
            }
            $base_prices_xml->asXML("./xml/base_prices.xml"); //Neues XML-Dokument speichern
        }

    }
    //Neuen Type hinzufuegen
    if(!empty($_POST['new_bez'])) {

        //Einfache Überprüfung der Eingabeparameter
        $type_bez = $_POST['new_bez'];

        if(is_numeric($_POST['new_value']))
            $value = $_POST['new_value'];
        else {
            $error = "Fehler bei Eingabedaten";
        }
        if(is_numeric($_POST['new_unit_price']))
            $unit_price = $_POST['new_unit_price'];
        else {
            $error = "Fehler bei Eingabedaten";
        }

        //ToDo: Datum muss noch ueberprueft werden
        $valid_from = $_POST['new_valid_from'];

        $types_xml = simplexml_load_file("./xml/types.xml");
        if($types_xml) {
            $max_id = count($types_xml->type)+1;
            foreach($types_xml->type as $type) {
                if($type->attributes()->type_name == $type_bez) {
                    $error = "Typ existiert bereits";
                }
            }
        }

        if(!isset($error)) {

            if($types_xml) {
                //Neuen Eintrag erzeugen
                $input = $types_xml->addChild('type');
                $input->addAttribute('type_id', $max_id);
                $input->addAttribute('type_name', $type_bez);
                $types_xml->asXML("./xml/types.xml"); //Neues XML-Dokument speichern;
            }

            $base_prices_xml = simplexml_load_file("./xml/base_prices.xml");
            if($base_prices_xml) {
                //Neuen Eintrag erzeugen
                $input = $base_prices_xml->addChild('base_price');
                $input->addAttribute('type', $type_bez);
                $input->addAttribute('value', $value);
                $input->addAttribute('unit_price', $unit_price);
                $input->addAttribute('valid_from', $valid_from);
                $base_prices_xml->asXML("./xml/base_prices.xml"); //Neues XML-Dokument speichern
            }
        }
        else {
            echo $error;
        }
    }
}

//Definierte Types auslesen und fuer Ausgabe in Array schreiben
$types_xml = simplexml_load_file("./xml/types.xml");
if($types_xml) {
    $i = 0;
    foreach($types_xml->type as $type) {
        $number = (int)$type->attributes()->type_id;
        $name = (string)$type->attributes()->type_name;
        $types[$number] = $name;
        $i++;
    }
}

//Daten zu den jeweiligen definierten Types auslesen
$base_prices_xml = simplexml_load_file("./xml/base_prices.xml");
if($base_prices_xml) {
    foreach($base_prices_xml->base_price as $base_price) {
        $name = (string)$base_price->attributes()->type;
        $value = (string)$base_price->attributes()->value;
        $valid_from = (string)$base_price->attributes()->valid_from;
        $unit_price = (string)$base_price->attributes()->unit_price;
        $base_prices[$name][name] = $name;
        $base_prices[$name][value] = $value;
        $base_prices[$name][valid_from] = $valid_from;
        $base_prices[$name][unit_price] = $unit_price;
    }
}

//Ausgabe aufbauen

foreach($types as $type) {
    echo '<form action="edit_types.php" method="POST">';
    echo ' Bezeichnung: <input name="type_bez" type="text" size="10" maxlength="10" value="'.$type.'" readonly>';
    echo ' Basispreis: <input name="value" type="text" size="10" maxlength="10" value="'.$base_prices[$type][value].'">';
    echo ' Einheitenpreis: <input name="unit_price" type="text" size="10" maxlength="10" value="'.$base_prices[$type][unit_price].'">';
    echo ' Basispreis gueltig ab: <input name="valid_from" type="text" size="10" maxlength="10" value="'.$base_prices[$type][valid_from].'">';
    echo ' Loeschen: <input type="checkbox" name="delete" value="'.$type.'">';
    echo '<input type="submit" value=" Edit ">';
    echo "<br />";
    echo '</form>';
}
echo '<form action="edit_types.php" method="POST">';
echo 'Neuer Eintrag';
echo '<br />';
echo '<br />';
echo ' Bezeichnung: <input name="new_bez" type="text" size="10" maxlength="10" value="">';
echo ' Basispreis: <input name="new_value" type="text" size="10" maxlength="10" value="">';
echo ' Einheitenpreis: <input name="new_unit_price" type="text" size="10" maxlength="10" value="">';
echo ' Basispreis gueltig ab: <input name="new_valid_from" type="text" size="10" maxlength="10" value="">';
echo '<input type="submit" value=" Add ">';
echo '<input type="reset" value=" Reset ">';
echo "<br />";
echo '</form>';

?>

<a href="http://localhost:8888/projekt/nkv">Zurueck zur Hauptseite</a>